#ifndef CSERIAL_H
#define CSERIAL_H

#include <QSerialPort>
#include <QString>
#include <QObject>

#ifdef Q_OS_LINUX
#   define __USE_MISC
#   include <linux/serial.h>
#   include <fcntl.h>
#   include <termios.h>
#   include <sys/ioctl.h>
#endif

class CSerial : public QObject
{
    Q_OBJECT
private:
#ifdef Q_OS_LINUX
    unsigned int convBaud(int nBaud);
#endif

    bool m_bIsOpened;
    QString m_qszPort;
    int m_nBaud;
    int m_nParity;
    int m_nStopBits;
    int m_nDataBits;

    QSerialPort::FlowControl m_flow;

    struct termios m_currTerm;
    struct termios m_backupTerm;

#ifdef Q_OS_LINUX
    int m_nFD;
#endif

public:

    CSerial();
    virtual ~CSerial();

    bool open();
    void close();
    bool isOpen();
    void setPortName(QString szPort);
    void setBaudRate(int nBaud);
    void setParity(int p);
    void setDataBits(int nBit);
    void setStopBits(int nBit);
    void setFlowControl(QSerialPort::FlowControl f);

    int write(char *ptr, int nLeng);
    int read(char *ptr, int nLimitLeng);
    void flush();

    QString portName();

signals:

public slots:

protected:

};

#endif // CSERIAL_H
